//
//  LZCouponListCellPresenter.m
//  laziz_Merchant
//
//  Created by xbnzd on 16/12/26.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZCouponListCellPresenter.h"
#import "LZCouponListCouponDetailModel.h"
#import "LZCouponListModel.h"
#import "LZCouponPresenter.h"
#import "LZAlertView.h"
#import "LZPlayVideoManager.h"
#import "LZVideoCacheManager.h"


@interface LZCouponListCellPresenter()
@property (nonatomic, strong) LZCouponPresenter *couponPresenter;

@end

@implementation LZCouponListCellPresenter


/**
 优惠操作 通用处理方法

 @param cCode 操作状态
 @param dataModel 数据源.列表和详情是不一样的model
 @param isDetailVC 是否是处理 优惠详情页中的操作
 @param successBlock <#successBlock description#>
 @param failureBlock <#failureBlock description#>
 */
- (void)dealClickCouponOperationButtonCode:(CouponListEventCode)cCode andModel:(id)dataModel isInCouponDetailVC:(BOOL)isDetailVC operationSuccess:(void (^)(void))successBlock Failure:(void (^)(NSString *))failureBlock
{
    NSString *couponIDStr = @"";
    if (isDetailVC) {
        LZCouponListCouponDetailModel *mTemp = (LZCouponListCouponDetailModel *)dataModel;
        couponIDStr = mTemp.couponId;
    }else{
        LZCouponListModel *mTemp = (LZCouponListModel *)dataModel;
        couponIDStr = mTemp.couponId;
    }
    
    WS(wSelf);
    switch (cCode) {
        case kCouponList_delete:{
            DLog(@"=======删除")
            LZCouponListModel *mTemp = (LZCouponListModel *)dataModel;
            
            LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"alert_delete_coupon", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"cancel", nil) otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
            [alert show];
            
            alert.clickButtonIndexBlock = ^(NSInteger index){
                if (index == 1) {
                    [wSelf.couponPresenter inCouponListModifyCouponWithCouponId:couponIDStr couponState:mTemp.state couponIsDelete:kCouponDelete_delete ResultSuccess:^(id data) {
                        
                        [[LZVideoCacheManager shareManager] removeTheLocalVideoWithVideoURL:mTemp.videoUrl];
                        
                        if (successBlock) {
                            successBlock();
                        }
                        
                    } Failure:^(NSString *fail) {
                        if (failureBlock) {
                            failureBlock(fail);
                        }
                    }];
                }
            };
            
        }
            break;
        case kCouponList_submit:{
            DLog(@"=======提交")
            LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"alert_submit_coupon", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"cancel", nil) otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
            [alert show];
            
            alert.clickButtonIndexBlock = ^(NSInteger index){
                if (index == 1) {
                    [wSelf.couponPresenter inCouponListModifyCouponWithCouponId:couponIDStr couponState:kCouponDetail_waitApproval couponIsDelete:kCouponDelete_notDelete ResultSuccess:^(id data) {
                        
                        if (successBlock) {
                            successBlock();
                        }
                        
                    } Failure:^(NSString *fail) {
                        if (failureBlock) {
                            failureBlock(fail);
                        }
                    }];
                }
            };
        }
            break;
        case kCouponList_publish:{
            DLog(@"=======发布")
            LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"alert_publish_coupon", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"cancel", nil) otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
            [alert show];
            
            alert.clickButtonIndexBlock = ^(NSInteger index){
                if (index == 1) {
                    [wSelf.couponPresenter inCouponListModifyCouponWithCouponId:couponIDStr couponState:kCouponDetail_published couponIsDelete:kCouponDelete_notDelete ResultSuccess:^(id data) {
                        
                        if (successBlock) {
                            successBlock();
                        }
                        
                    } Failure:^(NSString *fail) {
                        if (failureBlock) {
                            failureBlock(fail);
                        }
                    }];
                }
            };
        }
            break;
        case kCouponList_finish:{
            DLog(@"=======下线")
            LZAlertView *alert = [[LZAlertView alloc] initWithContent:LZLocalizedString(@"alert_finish_coupon", nil) andImage:nil cancelButtonTitle:LZLocalizedString(@"cancel", nil) otherButtonTitles:LZLocalizedString(@"confirm", nil),nil];
            [alert show];
            
            alert.clickButtonIndexBlock = ^(NSInteger index){
                if (index == 1) {
                    [wSelf.couponPresenter inCouponListModifyCouponWithCouponId:couponIDStr couponState:kCouponDetail_finished couponIsDelete:kCouponDelete_notDelete ResultSuccess:^(id data) {
                        
                        if (successBlock) {
                            successBlock();
                        }
                        
                    } Failure:^(NSString *fail) {
                        if (failureBlock) {
                            failureBlock(fail);
                        }
                    }];
                }
            };
        }
            break;
            
        default:
            break;
    }
}


- (LZCouponPresenter *)couponPresenter
{
    if (!_couponPresenter) {
        _couponPresenter = [[LZCouponPresenter alloc] init];
    }
    
    return _couponPresenter;
}
@end
